package com.cloudkinto.utils;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.cloudkinto.common.common.DateUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;

/**
 * @author fanp
 * @version v1.0.0
 * @Description :
 * @Create on : 2021/12/7 13:38
 **/
public class BeanChangeUtils<T> {
    public static String getValue(Object value) {
        String beforeValue = "";

        if (value instanceof Date) {
            beforeValue = DateUtils.dateFormatToString((Date) value);
        } else if (value == null) {
            beforeValue = "无";
        } else {
            beforeValue = value.toString();
        }
        return beforeValue;
    }

    /**
     * 此方法不能用链式编程
     *
     * @param oldBean
     * @param newBean
     * @param before
     * @param after
     */

    @SuppressWarnings({"unchecked", "rawtypes"})
    public void contrastObj(Object oldBean, Object newBean, Map<String, Object> before, Map<String, Object> after) {
        if (oldBean == null || newBean == null) {
            return;
        }

        T pojo1 = (T) oldBean;
        T pojo2 = (T) newBean;
        try {
            // 通过反射获取类的类类型及字段属性
            Class clazz = pojo1.getClass();
            Field[] fields = clazz.getDeclaredFields();
            int i = 1;
            for (Field field : fields) {
                // 排除序列化属性
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
                // 获取对应属性值
                Method getMethod = pd.getReadMethod();
                Object o1 = getMethod.invoke(pojo1);
                Object o2 = getMethod.invoke(pojo2);
                if (o1 == null && o2 == null) {
                    continue;
                }
                if ((o1 == null && o2.equals("")) || (o2 == null && o1.equals(""))) {
                    continue;
                }
                if (o1 == null) {
                    before.put(field.getName(), "无");
                    after.put(field.getName(), o2);
                    continue;
                }
                if (o2 == null) {
                    before.put(field.getName(), o1);
                    after.put(field.getName(), "无");
                    continue;
                }

                if (o1 instanceof String || o1 instanceof Integer || o1 instanceof Long
                        || o1 instanceof Float || o1 instanceof Double || o1 instanceof Char
                        || o1 instanceof Boolean || o1 instanceof Byte || o1 instanceof Short || o1 instanceof Date) {
                    if (!o1.toString().equals(o2.toString())) {
//                        if ((o1.equals("无") && o2.equals("")) || (o1.equals("") && o2.equals("无"))) {
//                            continue;
//                        }
                        before.put(field.getName(), o1);
                        after.put(field.getName(), o2);

                    }
                } else {
                    contrastObj(o1, o2, before, after);
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
